package fastcsv.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

public class GetFilesInfo {

	public static void main(String[] args) {
//		ArgumentsParser ap = ArgumentsParser.parse(args);
//		String path = ap.nokeyValues.size() == 0 ? "C:\\data" : ap.nokeyValues.get(0);
		File fp = new File(args.length > 0 ? args[0] : "C:\\data");
//		print("The input path is " + fp.getAbsolutePath());
		if (!fp.exists()) {
			print("The input path does not exist. ");
			return;
		}

		if (!fp.isDirectory()) {
			print("The input path is not a directory. ");
			return;
		}

		processPath(fp);
	}

	public static void processPath(File fp) {
		if (fp == null || !fp.exists()) {
			print(fp.getAbsolutePath() + " not existed");
			return;
		}

		if (fp.isFile()) {
			processFile(fp);
		} else if (fp.isDirectory()) {
			for (String subpath : fp.list()) {
				processPath(new File(fp, subpath));
			}
		}

	}

	public static void processFile(File file) {
		String[] data = new String[5];
		data[0] = file.length() + "";
		data[1] = file.lastModified() + "";
		try {
			var attrs = Files.readAttributes(Paths.get(file.getAbsolutePath()), BasicFileAttributes.class);
			data[2] = attrs.creationTime().toString();
			data[3] = attrs.lastAccessTime().toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		data[data.length - 1] = file.getAbsolutePath();
		print(String.join(",", data));
	}

	public static long getFileSize(String filename) {
		File file = new File(filename);
		if (!file.exists() || !file.isFile()) {
			System.out.println("文件不存在");
			return -1;
		}
		return file.length();
	}

	public static void print(String msg) {
		System.out.println(msg);
	}

}
